package com.integral.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.integral.biz.IBalanceBiz;
import com.integral.biz.IUserBiz;
import com.integral.biz.IUserTypeBiz;
import com.integral.entity.Balance;
import com.integral.entity.UserInfo;
import com.integral.entity.UserType;
import com.integral.vo.UserTypeVo;
import com.integral.vo.UserVo;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private IUserBiz userBiz;
	@Resource
	private IUserTypeBiz userTypeBiz;
	//注入用户余额
	@Resource
	private IBalanceBiz balanceBiz;

	/**
	 * 查询用户
	 * @param request
	 * @param response
	 * @param userVo
	 * @return
	 */
	@RequestMapping(value="/findUser")
	@ResponseBody
	public Map<String,Object> findUser(HttpServletRequest request, HttpServletResponse response,UserVo userVo){
		//设置查询未删除的数据
		userVo.setDeleteFlag(0);
		List<UserVo> list = userBiz.findUser(userVo);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", userBiz.findUserCount(userVo));
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 查询用户类别
	 * @param request
	 * @param response
	 * @param userVo
	 * @return
	 */
	@RequestMapping(value="/findUserType")
	@ResponseBody
	public Map<String,Object> findUserType(HttpServletRequest request, HttpServletResponse response,UserTypeVo userTypeVo){
		//设置查询未删除的数据
		userTypeVo.setDeleteFlag(0);
		List<UserTypeVo> list = userTypeBiz.findUserType(userTypeVo);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", userTypeBiz.findUserTypeCount(userTypeVo));
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 新增用户类别
	 * @param userTypeVo
	 * @return
	 */
	@RequestMapping(value="/addUserType")
	public String addUserType(UserTypeVo userTypeVo){
		userTypeBiz.addUserType(userTypeVo);
		return "management/userType";
	}
	/**
	 * 修改用户类别
	 * @param userTypeVo
	 * @return
	 */
	@RequestMapping(value="/updateUserType")
	public String updateUserType(UserTypeVo userTypeVo){
		UserType userType = new UserType();
		userType.setId(userTypeVo.getId());
		userType.setUserAward(userTypeVo.getUserAward());
		userTypeBiz.updateUserType(userType);
		return "management/userType";
	}
	/**
	 * 修改用户余额
	 * @return
	 */
	@RequestMapping(value="/updateBalance")
	@ResponseBody
	public String updateBalance(HttpServletRequest request,Balance balance){
		//获取登录对象
		HttpSession session = request.getSession();
		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		
		balance.setUpdateEmp(user.getUserName());
		balance.setUpdateTime(new Date());
		int i = balanceBiz.updateByUserCode(balance);
		if(i>0){
			return "修改成功！";
		}else{
			return "修改失败！";
		}
	}
	
}
